﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using ERPStudio;
using System.Configuration;
using System.Threading;
using System.Globalization;
using System.Windows;
//using System.Windows.Threading;


namespace ERPStudio
{

    public struct TreeNodeTag
    {
        public EventHandler handler;
        public ToolStripMenuItem menu;
        public TreeNodeTag(EventHandler h, ToolStripMenuItem m) { handler = h; menu = m; }
    };

    public class ModuleClass
    {
        /// daca arhiva e editabila
        public Boolean eArhivaEditabila = false;
        /// <summary>
        /// Conexiunea implicita 
        /// </summary>
        private String _workConnectionString="ESRPConnectionString";
        public String workConnectionString
        {
            get { return _workConnectionString; }
            set { _workConnectionString = value; }
        }

        public String CurrentTheme = String.Empty;
        public FrmMainMdi mdiForm = null;
        public Autorizare oAuth = new Autorizare();
        public Int16 StareAutorizare = 0; //demonstrativ
        /// <summary>
        /// Numele Modulului cu litere mici
        /// </summary>
        private String _ModuleName = String.Empty;
        public String ModuleName
        {
            get { return _ModuleName; }
            set { _ModuleName = value; }
        }

        /// <summary>
        /// ID-ul aplicatiei
        /// </summary>
        private Guid _appID = Guid.Empty;
        public Guid ApplicationID
        {
            get { return _appID; }
        }
        /// <summary>
        /// id-ul utilizatorului
        /// </summary>
        private Guid _userID = Guid.Empty;
        public Guid UserID
        {
            set { _userID = value; }
            get { return _userID; }
        }
        
        /// <summary>
        /// indexul benzii din Bara de Navigatie
        /// </summary>
        private int _NaviBarBandIndex;
        public int NaviBarBandIndex
        {
            set { _NaviBarBandIndex = value; }
            get { return _NaviBarBandIndex; }
        }

        /// <summary>
        /// Tree View din Bara de Navigatie
        /// </summary>
        private TreeView _NaviBarTreeView;
        public TreeView NaviBarTreeView
        {
            set { _NaviBarTreeView = value; }
            get { return _NaviBarTreeView; }
        }


        public ModuleClass(Int16 codUnit,String numeUnit,String theme)
        {
            CodUnitate = codUnit;
            NumeUnitate = numeUnit;
            CurrentTheme = theme;
        }

        public ModuleClass()
        {
            CodUnitate = 0;
            NumeUnitate = String.Empty;
            CurrentTheme = String.Empty;
        }

        public void ChangeUnit(Int16 codUnit,String numeUnit)
        {
            CodUnitate = codUnit;
            NumeUnitate = numeUnit;
        }

        virtual public String GetModuleID()
        {
            return "";
        }

        public String GetModuleName()
        {
            return "No name";
        }

        public static void MyTrace(String val)
        {
            Trace.WriteLine("[" + DateTime.Now.ToString() + "]" + val);
        }
        public Int16 CodUnitate = 0;
        public String NumeUnitate = String.Empty;
        /// <summary>
        /// contine anul arhivei care se vizualizeaza
        /// daca e vid - perioada curenta
        /// </summary>
        public String AnArhiva = String.Empty;

        public ToolStripMenuItem menuMain = null;
        
        public static void InitStoredProcedure(String nume)
        {
            SqlCommand cmd = null;
            try
            {
                cmd = new SqlCommand(nume, StudioForm.DeschideConexiune("ESRPConnectionString"));
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("versiune", "1.0.0.2"));
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                StudioForm.InchideConexiune(cmd.Connection);
            }
        }

        public static void InitStoredProcedure(String nume,String codunitate)
        {
            SqlCommand cmd = null;
            try
            {
                cmd = new SqlCommand(nume, StudioForm.DeschideConexiune("ESRPConnectionString"));
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("versiune", "1.0.0.2"));
                if(codunitate!=String.Empty)
                    cmd.Parameters.Add(new SqlParameter("codunitate", codunitate));
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                StudioForm.InchideConexiune(cmd.Connection);
            }
        }

        public ToolStripMenuItem FindMenuItem(ToolStripMenuItem mn, String nume)
        {
            if (mn == null)
            {
                MessageBox.Show("Eroare interna - meniul nu a fost setat");
                return null;
            }
            ToolStripItem[] ret = mn.DropDownItems.Find(nume,true);
            if (ret.Length > 0)
                return ((ToolStripMenuItem)ret[0]);
            return null; 
        }

        virtual public void AfterLoadMenu(ToolStripMenuItem mn, FrmMainMdi vForm, Guid appID, Guid userID)
        {
            menuMain = mn;        
        }

        virtual public void LoadMenu(ToolStripMenuItem mn, FrmMainMdi vForm, Guid appID, Guid userID)
        {
            _appID = appID;
            _userID = userID;
            //seteaza forma parinte pentru machete
            mdiForm = vForm;
            menuMain = mn;      
        }

        public ToolStripMenuItem AddMenu(ToolStripMenuItem menuParinte, String Nume, EventHandler funcButon,TreeNode parinte, out TreeNode tn)
        {
            tn = null;
            if (menuParinte == null)
                return null;
            ToolStripMenuItem mn1 = (ToolStripMenuItem)menuParinte.DropDownItems.Add(Nume);
            if (mn1 == null)
                return null;
            if(funcButon!=null)
                mn1.Click += new System.EventHandler(funcButon);
            
            if (parinte != null)
            {
                tn = parinte.Nodes.Add(Nume);
                if (funcButon != null)
                {
                    //Delegate temp = Delegate.CreateDelegate(typeof(EventHandler), modInfo.theMCObject, MethodName);
                    tn.Tag = new TreeNodeTag((EventHandler)funcButon, mn1);
                }
            }

            return mn1;
        }

        public void SetNaviBarInfo(int naviBandidx,TreeView naviTreeView)
        {
            NaviBarBandIndex = naviBandidx;
            NaviBarTreeView = naviTreeView;
        }


    }
}
